package basic;

import finds.Finder;
import helper.Helper;
import io.appium.java_client.android.AndroidDriver;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import testcase.login.LoginCase;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BasicCase {

    protected static  AndroidDriver<WebElement> driver;
    public Finder finder;
    protected Helper helper;

    @BeforeMethod
    public void setUp() throws MalformedURLException, InterruptedException {
        DesiredCapabilities cap = new DesiredCapabilities();
        File classpathRoot =new File(System.getProperty("user.dir"));
        File app=new File(classpathRoot,"apk/IOT_1.0_8a88f19_180912.apk");
        cap.setCapability("platformVersion", "8.1.0");
        cap.setCapability("deviceName", "4be8aaa6");
        cap.setCapability("appActivity", "com.oppo.ohome.LaunchActivity");
        cap.setCapability("app",app.getAbsolutePath());
        cap.setCapability("platformName", "Android");
        cap.setCapability("noReset",true);
        cap.setCapability("unicodeKeyboard",true);
        cap.setCapability("resetKeyboard",true);
        cap.setCapability("appWaitActivity", ".MainActivity");
        cap.setCapability("automationName", "uiautomator2");
        driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        finder=new Finder(driver);
        helper=new Helper(finder);

        //首次启动的隐私声明页做处理
        try {
            if (driver.findElement(By.id("com.oppo.ohome:id/tv_desc")).isDisplayed()) {
                driver.findElementById("com.oppo.ohome:id/tv_agree").click();
            }
            //对首次启动app时出现的权限请求弹框进行处理
            if(driver.findElementByName("“智能家居”需要使用位置信息权限，您是否允许？").isDisplayed()){
                driver.findElement(By.xpath(".//*[@text='始终允许']")).click();//同意权限请求
            }
            if(driver.findElement(By.name("“智能家居”需要使用存储空间权限，您是否允许？")).isDisplayed()){
                driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button")).click();
            }
            if(driver.findElement(By.name("“智能家居”需要使用电话权限，您是否允许？")).isDisplayed()){
                driver.findElement(By.xpath(".//*[@text='始终允许']")).click();
            }
            if (driver.findElement(By.name("“智能家居”需要使用相机权限，您是否允许？")).isDisplayed()){
                driver.findElement(By.xpath("始终允许")).click();
            }
            if(driver.findElement(By.name("声明与条款")).isDisplayed()){
                driver.findElement(By.name("同意并继续")).click();
                //等待回到首页后再操作
                finder.waitforWebElement("com.oppo.ohome:id/tv_title_sub");
            }

        }catch(Exception e){
            System.out.println("非首次启动app");
        }

        //判断是否登录
        LoginCase.isNeedLogin();

    }

//    @Test
//    public void testName() throws InterruptedException {
//        Thread.sleep(6000);
//        driver.findElementById("com.oppo.ohome:id/iv_add").click();
//
//    }

    @AfterMethod
    public void tearDown() {
        //异常处理
       try{
           driver.quit();
           driver.execute("am force-stop com.coloros.gallery3d");
           driver.execute("am force-stop com.oppo.usercenter");
       }catch(Exception e){
           e.printStackTrace();
       }
    }

    //错误截图
    public static void captrueScreenShot(ITestResult result){
        String passfailMethod=result.getMethod().getRealClass().getSimpleName()+"."+result.getMethod().getMethodName();
        File scrFile=((TakesScreenshot)BasicCase.driver).getScreenshotAs(OutputType.FILE);
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String destDir="screenshots/Failures";
        new File(destDir).mkdirs();
        String destFile=passfailMethod+"-"+dateFormat.format(new Date())+".png";
        try {
            FileUtils.copyFile(scrFile,new File(destDir+"/"+destFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
